const devBaseUrl = "http://8.130.64.125:8099/";




const httpGet = (url, data, checkToken = true) => {
	uni.onNetworkStatusChange(function(res) {
		if (!res.isConnected) {
			uni.showToast({
				title: '网络连接不可用！',
				icon: 'none'
			});
		}
		return false
	});

	let token = uni.getStorageSync('erpToken');
	if (checkToken) {
		if (token == '' || token == undefined || token == null) {
			uni.showToast({
				title: '账号已过期，请重新登录',
				icon: 'none',
				complete: function() {
					uni.reLaunch({
						url: '/pages/login/index'
					});
				}
			});
		}
	}

	let baseUrl = devBaseUrl;
	if (process.env.NODE_ENV === 'production') {
		baseUrl = proBaseUrl
	}
	console.log(baseUrl);
	let httpDefaultOpts = {
		url:  baseUrl + url,
		data: data,
		method: 'GET',
		header: {
			'Access-Token': token,
			'X-Requested-With': 'XMLHttpRequest',
			"Accept": "application/json",
			"Content-Type": "application/json; charset=UTF-8"
		},
		dataType: 'json',
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			res => {
				resolve(res[1].data);
			}
		).catch(
			(response) => {
				reject(response)
			}
		)
	})
	return promise
};

const httpPost = (url, data, checkToken = true) => {
	uni.onNetworkStatusChange(function(res) {
		if (!res.isConnected) {
			uni.showToast({
				title: '网络连接不可用！',
				icon: 'none'
			});
		}
		return false
	});

	let token = uni.getStorageSync('erpToken');
	if (checkToken) {
		if (token == '' || token == undefined || token == null) {
			uni.showToast({
				title: '账号已过期，请重新登录',
				icon: 'none',
				complete: function() {
					uni.reLaunch({
						url: '/pages/login/login'
					});
				}
			});
		}
	}

	let baseUrl = devBaseUrl;
	if (process.env.NODE_ENV === 'production') {
		baseUrl = proBaseUrl
	}
	let httpDefaultOpts = {
		url: baseUrl + url,
		data: data,
		method: 'POST',
		header: {
			'Access-Token': token,
			'X-Requested-With': 'XMLHttpRequest',
			"Accept": "application/json",
			"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
		},
		dataType: 'json',
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				if (res[1].data.httpCode == 401) {
					uni.reLaunch({
						url: '/pages/login/login'
					});
				}
				resolve(res[1].data);
			}
		).catch(
			(response) => {
				reject(response)
			}
		)
	})
	return promise
};

const httpPostBody = (url, data, checkToken = true) => {
	uni.onNetworkStatusChange(function(res) {
		if (!res.isConnected) {
			uni.showToast({
				title: '网络连接不可用！',
				icon: 'none'
			});
		}
		return false
	});
	let token = uni.getStorageSync('erpToken');
	if (checkToken) {
		if (token == '' || token == undefined || token == null) {
			uni.showToast({
				title: '账号已过期，请重新登录',
				icon: 'none',
				complete: function() {
					uni.reLaunch({
						url: '/pages/login/index'
					});
				}
			});
			return;
		}
	}


	let baseUrl = devBaseUrl;
	if (process.env.NODE_ENV === 'production') {
		baseUrl = proBaseUrl
	}
	let httpDefaultOpts = {
		url: baseUrl + url,
		data: data,
		method: 'POST',
		header: {
			'Access-Token': token,
			'X-Requested-With': 'XMLHttpRequest',
			"Accept": "application/json",
			"Content-Type": "application/json; charset=UTF-8"
		},
		dataType: 'json',
	}
	let promise = new Promise(function(resolve, reject) {
		uni.request(httpDefaultOpts).then(
			(res) => {
				resolve(res[1].data);
			}
		).catch(
			(response) => {
				reject(response)
			}
		)
	})
	return promise
};

export default {	
	httpGet,
	httpPost,
	httpPostBody,
}
